package LinkedList.SingleLinkedList;

import java.util.List;

public class SingleLinkedList {
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    //头插法
    public void addFirst(int data){
        ListNode node = new ListNode(data);
        node.next = head;
        head=node;
    }
    //尾插法
    public void addLast(int data){
        ListNode node = new ListNode(data);
        ListNode now = head;
        if(now==null) {//如果列表一个节点都没有，这样可以避免now指向null
            head = node;
            return;
        }
        while (now.next!=null) {
            now=now.next;
        }
        now.next=node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index>size() || index<0) {
            System.out.println("位置不合法");
            return;
        }
        if(index==0) {
            addFirst(data);
        }
        if(index==size()) {
            addLast(data);
        }
        ListNode now = findIndexPre(index);
        ListNode node = new ListNode(data);
        node.next=now.next;
        now.next=node;
    }
    public ListNode findIndexPre(int index) {
        ListNode now = head;
        while (index-1!=0) {
            now=now.next;
            index--;
        }
        return now;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNode now = head;
        while(now!=null) {
            if(now.val==key) {
                return true;
            }
            now=now.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        ListNode now = findPre(key);
        if(now == null) {
            System.out.println("没有你要删除的数据");
            return;
        }
        ListNode delete = now.next;
        now.next=delete.next;
    }
    public ListNode findPre(int key) {
        ListNode now = head;
        while(now.next!=null) {
            if(now.next.val == key) {
                return now;
            }
            now= now.next;
        }
        return null;
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode now = head.next;
        ListNode pre = head;
        while(now!=null) {
            if(now.val==key) {
                pre.next=now.next;
                now=now.next;
            }
            else {
                pre=now;
                now=now.next;
            }
        }
    }
    //得到单链表的长度
    public int size(){
        ListNode now = head;
        int count = 0;
        while(now!=null) {
            count++;
            now=now.next;
        }
        return count;
    }
    public void display(){
        ListNode now = head;
        while(now!=null) {
            System.out.print(now.val+" ");
            now=now.next;
        }
        System.out.println();
    }
    public void clear(){
        head.next=null;
    }

    public void GenerateList() {
        ListNode node1 = new ListNode(11);
        ListNode node2 = new ListNode(22);
        ListNode node3 = new ListNode(33);
        ListNode node4 = new ListNode(44);
        ListNode node5 = new ListNode(55);
        node1.next=node2;
        node2.next=node3;
        node3.next=node4;
        node4.next=node5;
        this.head=node1;
    }
    public ListNode FindKFromBack(int k) {
        if(k<=0||head==null) {
            return null;
        }
        ListNode fast = head;
        ListNode slow = head;
        for (int i = 0; i < k-1; i++) {
            fast=fast.next;
            if(fast==null) {
                return null;
            }
        }
        while(fast.next!=null) {
            slow=slow.next;
            fast=fast.next;
        }
        return slow;
    }

    public static void main(String[] args) {
        SingleLinkedList SL1 = new SingleLinkedList();
        //singleLinkedList.GenerateList();
        SL1.addFirst(11);
        SL1.addFirst(22);
        SL1.addFirst(33);
        SL1.addFirst(44);
        SL1.addFirst(55);
        SL1.addLast(11);
        //singleLinkedList.remove(11);
        //singleLinkedList.removeAllKey(11);
        SL1.display();
        SingleLinkedList SL2 = new SingleLinkedList();
        SL2.addLast(12);
        SL2.addLast(23);
        SL2.addLast(34);
        SL2.addLast(45);
        ListNode ret = SL1.FindKFromBack(2);
        System.out.println(ret.val);
    }
}
